<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      @ok="handleOk"
      @cancel="handleClose"
      :destroyOnClose="true"
      :visible="visible"
      :confirm-loading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-row>
            <!-- line-1 -->
            <a-col :span="21">
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="员工姓名"
                    prop="name"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入员工姓名"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="用户名"
                    prop="userName"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入用户名"
                      v-model.trim="model.userName"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="邮箱"
                    prop="email"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入邮箱"
                      v-model.trim="model.email"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <a-row :gutter="24">
                <!-- line-2 -->
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="员工编号"
                    prop="employeeNumber"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入员工编号"
                      v-model.trim="model.employeeNumber"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="部门"
                    prop="department"
                    hasFeedback
                  >
                    <a-tree-select
                      v-model.trim="model.department"
                      style="width: 100%"
                      :dropdown-style="{
                        maxHeight: '400px',
                        overflow: 'auto',
                      }"
                      :tree-data="treeData"
                      :replaceFields="replaceFields"
                      placeholder="请选择部门"
                      tree-default-expand-all
                    ></a-tree-select>
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="角色"
                    prop="roleId"
                    hasFeedback
                  >
                    <a-select
                      v-model.trim="model.roleId"
                      placeholder="请选择角色"
                    >
                      <a-select-option
                        v-for="item in RoleOption"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <!-- line-3 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="手机号"
                    prop="phone"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入手机号"
                      v-model.trim="model.phone"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="21">
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="出生日期"
                    prop="nae"
                    hasFeedback
                  >
                    <a-date-picker
                      :showToday="false"
                      placeholder="请选择出生日期"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="职务"
                    prop="nae"
                    hasFeedback
                  >
                    <a-select placeholder="请选择职务">
                      <a-select-option value="lucy">
                        Lucy
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="所学专业"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入所学专业"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <!-- line-5 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="籍贯"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入籍贯"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="入职日期"
                    prop="nae"
                    hasFeedback
                  >
                    <a-date-picker
                      :showToday="false"
                      placeholder="请选择入职日期"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="教育程度"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入教育程度"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-6 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="性别"
                    prop="nae"
                    hasFeedback
                  >
                    <CustomRadio
                      :option="genderOption"
                      name="nameCount"
                      v-model.trim="model.nameCount"
                      @changeRadio="changeRadio"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="试用期天数"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入试用期天数"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="合同到期日期"
                    prop="nae"
                    hasFeedback
                  >
                    <a-date-picker
                      :showToday="false"
                      placeholder="请选择合同到期日期"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-7 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="民族"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入籍贯"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="离职日期"
                    prop="nae"
                    hasFeedback
                  >
                    <a-date-picker
                      :showToday="false"
                      placeholder="请选择离职日期"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="工资银行账号"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入工资银行账号"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-8 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="身份证号"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入身份证号"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="备注"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入身份证号"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="医疗保险账号"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入医疗保险账号"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-9 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="家庭住址"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入家庭住址"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="婚姻状况"
                    prop="nae"
                    hasFeedback
                  >
                    <CustomRadio
                      :option="marriageOption"
                      name="nameCount"
                      v-model.trim="model.nameCount"
                      @changeRadio="changeRadio"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="养老保险账号"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入养老保险账号"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-10 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="现住地址"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入现住地址"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="婚姻状况"
                    prop="nae"
                    hasFeedback
                  >
                    <CustomRadio
                      :option="politicsOption"
                      name="nameCount"
                      v-model.trim="model.nameCount"
                      @changeRadio="changeRadio"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="职称"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入职称"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>

              <!-- line-11 -->
              <a-row :gutter="24">
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="家庭电话"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入家庭电话"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="QQ号码"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入QQ号码"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="其他信息"
                    prop="nae"
                    hasFeedback
                  >
                    <a-input
                      placeholder="请输入其他信息"
                      v-model.trim="model.name"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-col>
            <!-- 上传区域 -->
            <a-col :span="3">
              <a-upload
                name="avatar"
                list-type="picture-card"
                class="avatar-uploader"
                :show-upload-list="false"
              >
                <div>
                  <div class="ant-upload-text">
                    Upload
                  </div>
                </div>
              </a-upload>
            </a-col>
          </a-row>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import CustomRadio from "@/components/form/CustomRadio";

import api from "@/api/apiServer";

export default {
  props: {
    treeData: {
      type: Array,
    },
  },
  components: { CustomRadio },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 1677, // 抽屉宽度
      model: {
        name: "",
      },
      show: true, //根据菜单类型，动态显示隐藏表单元素
      roleLabel: "角色名称",
      menuLabel: "菜单名称",
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      confirmLoading: false,
      iconChooseVisible: false,
      // 状态数据源
      statusOption: [
        { label: "在职", value: "1" },
        { label: "离职", value: "2" },
      ],
      // 性别数据源
      genderOption: [
        { label: "男", value: "1" },
        { label: "女", value: "2" },
      ],
      // 婚姻数据源
      marriageOption: [
        { label: "未婚", value: "1" },
        { label: "已婚", value: "2" },
      ],
      // 婚姻数据源
      politicsOption: [
        { label: "群众", value: "1" },
        { label: "党员", value: "2" },
      ],
      RoleOption: [],
      replaceFields: {
        title: "departmentName",
        key: "id",
        value: "id",
        children: "childList",
      },
      validateStatus: "",
      autoExpandParent: true,
      selectedKeys: [],
    };
  },
  computed: {
    rules() {
      return {
        name: [
          { required: true, message: "请输入员工姓名", trigger: "change" },
        ],
        userName: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        employeeNumber: [
          { required: true, message: "请输入员工编号", trigger: "change" },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "change" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "change" }],
        department: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        roleId: [{ required: true, message: "请选择角色", trigger: "change" }],
      };
    },
  },
  methods: {
    changeRadio(name, value) {
      this.model[name] = value;
    },
    async getRole() {
      const res = await api.RoleFindAll();
      this.RoleOption = res.data;
    },
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.getRole();
      this.visible = true;
      this.model = Object.assign({}, record);
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res;
      if (!this.model.id) {
        res = await api.UserAdd(this.model);
      } else {
        res = await api.UserEdit(this.model);
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.confirmLoading = false;
      this.$message.success(res.message);
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 1677;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.formBox {
  width: 100%;
  border: 1px solid #e9e9e9;
  padding: 10px 16px;
  margin: 18px 0;
  .requiredNews {
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 600;
    color: #1a5eff;
    margin-bottom: 12px;
  }

  .ant-calendar-picker {
    width: 100%;
  }
}
</style>
